//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class ProductVariantAttributeValueDBManager
    {
        #region Methods
        public static void DeleteProductVariantAttributeValue(int ProductVariantAttributeValueID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariantAttributeValueDelete");
            db.AddInParameter(dbCommand, "ProductVariantAttributeValueID", DbType.Int32, ProductVariantAttributeValueID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static ProductVariantAttributeValueCollection GetAllByByProductVariantAttributeID(int ProductVariantAttributeID)
        {
            ProductVariantAttributeValueCollection productVariantAttributeValues = new ProductVariantAttributeValueCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariantAttributeValueLoadByProductVariantAttributeID");
            db.AddInParameter(dbCommand, "ProductVariantAttributeID", DbType.Int32, ProductVariantAttributeID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    ProductVariantAttributeValue productVariantAttribute = new ProductVariantAttributeValue();
                    productVariantAttribute.ProductVariantAttributeValueID = NopDataHelper.GetInt(dataReader, "ProductVariantAttributeValueID");
                    productVariantAttribute.ProductVariantAttributeID = NopDataHelper.GetInt(dataReader, "ProductVariantAttributeID");
                    productVariantAttribute.Name = NopDataHelper.GetString(dataReader, "Name");
                    productVariantAttribute.PriceAdjustment = NopDataHelper.GetDecimal(dataReader, "PriceAdjustment");
                    productVariantAttribute.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    productVariantAttributeValues.Add(productVariantAttribute);
                }
            }
            return productVariantAttributeValues;
        }

        public static ProductVariantAttributeValue GetByProductVariantAttributeValueID(int ProductVariantAttributeValueID)
        {
            ProductVariantAttributeValue productVariantAttribute = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariantAttributeValueLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ProductVariantAttributeValueID", DbType.Int32, ProductVariantAttributeValueID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    productVariantAttribute = new ProductVariantAttributeValue();
                    productVariantAttribute.ProductVariantAttributeValueID = NopDataHelper.GetInt(dataReader, "ProductVariantAttributeValueID");
                    productVariantAttribute.ProductVariantAttributeID = NopDataHelper.GetInt(dataReader, "ProductVariantAttributeID");
                    productVariantAttribute.Name = NopDataHelper.GetString(dataReader, "Name");
                    productVariantAttribute.PriceAdjustment = NopDataHelper.GetDecimal(dataReader, "PriceAdjustment");
                    productVariantAttribute.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                }
            }
            return productVariantAttribute;
        }

        public static ProductVariantAttributeValue InsertProductVariantAttributeValue(int ProductVariantAttributeID,
            string Name,decimal PriceAdjustment, int DisplayOrder)
        {
            ProductVariantAttributeValue productVariantAttributeValue = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariantAttributeValueInsert");
            db.AddOutParameter(dbCommand, "ProductVariantAttributeValueID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "ProductVariantAttributeID", DbType.Int32, ProductVariantAttributeID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "PriceAdjustment", DbType.Decimal, PriceAdjustment);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int ProductVariantAttributeValueID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ProductVariantAttributeValueID"));
                productVariantAttributeValue = GetByProductVariantAttributeValueID(ProductVariantAttributeValueID);
            }
            return productVariantAttributeValue;
        }

        public static ProductVariantAttributeValue UpdateProductVariantAttributeValue(int ProductVariantAttributeValueID,
            int ProductVariantAttributeID, string Name, decimal PriceAdjustment, int DisplayOrder)
        {
            ProductVariantAttributeValue productVariantAttributeValue = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ProductVariantAttributeValueUpdate");
            db.AddInParameter(dbCommand, "ProductVariantAttributeValueID", DbType.Int32, ProductVariantAttributeValueID);
            db.AddInParameter(dbCommand, "ProductVariantAttributeID", DbType.Int32, ProductVariantAttributeID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "PriceAdjustment", DbType.Decimal, PriceAdjustment);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                productVariantAttributeValue = GetByProductVariantAttributeValueID(ProductVariantAttributeValueID);
            return productVariantAttributeValue;
        }
        #endregion
    }
}
